package com.woniuxy.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.goods.entity.UpgoodsLog;
import com.woniuxy.goods.mapper.UpgoodsLogMapper;
import com.woniuxy.goods.param.AddUpGoodsLogParam;
import com.woniuxy.goods.service.UpgoodsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.web.util.ConvertUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * 典当物品日志管理服务层实现类
 * @author ...
 * @since 2024年05月22日
 */
@Service
public class UpgoodsLogServiceImpl extends ServiceImpl<UpgoodsLogMapper, UpgoodsLog> implements UpgoodsLogService {

    /**
     * 新增典当物品日志记录
     * @param param 新增物品日志对象
     * @return 物品日志对象
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public UpgoodsLog addUpGoodsLog(AddUpGoodsLogParam param) {
        UpgoodsLog log = BeanUtil.toBean(param, UpgoodsLog.class);
        log.setModifyTime(LocalDateTime.now().toString());
        save(log);
        return log;
    }

    /**
     * 批量新增典当物品日志记录
     * @param params 新增物品日志对象集合
     * @return 物品日志对象集合
     * @throws Exception
     */
    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public List<UpgoodsLog> addUpGoodsLogList(List<AddUpGoodsLogParam> params) {
        List<UpgoodsLog> list = ConvertUtil.convertListTo(params, UpgoodsLog::new);
        for (UpgoodsLog upgoodsLog : list) {
            upgoodsLog.setModifyTime(LocalDateTime.now().toString());
        }
        saveBatch(list);
        return list;
    }

    /**
     * 通过物品id典当物品日志记录
     * @param goodsId 通过物品id
     * @return 物品日志对象集合
     * @throws Exception
     */
    @Override
    public List<UpgoodsLog> findUpGoodsLog(Integer goodsId) {
        QueryWrapper<UpgoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        return list(wrapper);
    }
}
